import homeRouter from '../modules/majom';
import { store } from '@/redux';
import { ADMIN_AUTH_KEY, SUPER_ADMIN_AUTH_KEY } from '@conf';

const keysDict: any = {};
const breadcrumbDict: any = {};
let menulist: any = [];
let saveIndex: number;

function getMenus(arr: any, father_path: any) {
	const menus: any = [];
	arr.forEach(({ path, meta: { title, icon, hidden }, children }: any) => {
		path = father_path ? `${father_path}/${path}` : path;
		keysDict[path] = title;
		breadcrumbDict[path] = [
			{
				path: father_path,
				label: keysDict[father_path],
			},
			{
				path,
				label: title,
			},
		];
		if (!hidden) {
			const menu: any = {
				key: path,
				icon,
				label: title,
			};
			if (children) menu.children = getMenus(children, path);
			menus.push(menu);
		}
	});
	return menus;
}

export default function () {
	const { authLevel } = store.getState().global;
	// const levelIndex = authLevel == SUPER_ADMIN_AUTH_KEY ? 1 : 0;
	const levelIndex = 0;
	if (!menulist || (!menulist.length && saveIndex != levelIndex)) {
		saveIndex = levelIndex;
		// keysDict = {};
		// breadcrumbDict = {};
		// menulist = [];
		const { children: MainRouter, path } = homeRouter[levelIndex];
		menulist = getMenus(MainRouter, path);
	}
	return {
		menulist,
		menudict: keysDict,
		breadcrumbdict: breadcrumbDict,
	};
}
